<template>
    <el-form-item label="广告组预算" :prop="`adgroup.delivery.${dataForm.delivery_index}.budget`" :rules="dataForm.adgroup.delivery[dataForm.delivery_index].budget_mode=='BUDGET_MODE_DAY'?createRules.checkPrice:createRules.checkPriceAll">
        <div class="flexbox flexcenter">
            <el-input :placeholder="dataForm.adgroup.delivery[dataForm.delivery_index].budget_mode=='BUDGET_MODE_DAY'?'20以上':'280以上'" style="width:246px" v-model="dataForm.adgroup.delivery[dataForm.delivery_index].budget" class="tk_input" @change="sessionSave">
                <span slot="suffix" style="line-height:32px;color:#17233D;margin-right:5px">USD</span>
                <el-select v-model="dataForm.adgroup.delivery[dataForm.delivery_index].budget_mode" slot="prepend" placeholder="" class="tk_select" @change="changeBdMode">
                    <el-option label="日预算" value="BUDGET_MODE_DAY"></el-option>
                    <el-option label="总预算" value="BUDGET_MODE_TOTAL"></el-option>
                </el-select>
            </el-input>
            <el-link type="primary" :underline="false" style="margin-left:12px" @click="addBulkBid">
                <div class="flexbox flexcenter">
                    <img src="../../../assets/img/offer_add.svg" alt="" style="margin-right:5px">
                    批量出价
                </div>
            </el-link>
            <div class="flexbox flexcenter red" v-if="dataForm.campaign.budget_mode_status=='BUDGET_MODE_DAY'">
                <i class="el-icon-warning-outline"></i>
                注意：广告系列预算已开启,预算类型请与广告系列预算类型保持一致。
            </div>
        </div>
    </el-form-item>
</template>

<script>

export default {
    props: ["dataForm"],
    components: {},
    name: "",
    data() {
        var checkPrice = (rule, value, callback) => {
            let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            let price = Number(value)
            if (!reg.test(price)) {
                return callback(new Error("日预算应为20.00 - 9,999,999.99间"));
            } else if (price < 20 || price > 9999999) {
                return callback(new Error("日预算应为20.00 - 9,999,999.99间"));
            } else {
                callback();
            }
        };
        var checkPriceAll = (rule, value, callback) => {
            let reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
            let price = Number(value)
            if (!reg.test(price)) {
                return callback(new Error("总预算应为280.00 - 9,999,999.99间"));
            } else if (price < 280 || price > 9999999) {
                return callback(new Error("总预算应为280.00 - 9,999,999.99间"));
            } else {
                callback();
            }
        };
        return {
            noset: "",
            createRules:{
                checkPrice: [
                    {required: true,message: "请输入",trigger: ["blur", "change"],},
                    { validator: checkPrice, trigger: ["blur", "change"] },
                ],
                checkPriceAll: [
                    {required: true,message: "请输入",trigger: ["blur", "change"],},
                    { validator: checkPriceAll, trigger: ["blur", "change"] },
                ],
            }
        };
    },
    created() {},
    methods: {
        sessionSave(){
            this.$emit("sessionSave")
        },
        changeBdMode(val){
            if(val=='BUDGET_MODE_TOTAL'){
                this.dataForm.adgroup.delivery[this.dataForm.delivery_index].schedule_type = 'SCHEDULE_START_END'
            }
            this.sessionSave()
        },

        addBulkBid(){
            if(this.dataForm.adgroup.delivery.length>=20){
                this.$message.error("预算分组已超出最大限制");
                return;
            }
            if(!this.dataForm.adgroup.delivery[this.dataForm.delivery_index].budget){
                this.$message.error("请输入预算");
                return;
            }
            if(this.dataForm.adgroup.delivery[this.dataForm.delivery_index].deep_bid_type=="VO_MIN_ROAS"){
                if(!this.dataForm.adgroup.delivery[this.dataForm.delivery_index].roas_bid){
                    this.$message.error("请输入广告花费回报（ROAS）下限值");
                    return;
                }   
            }
            if(this.dataForm.adgroup.delivery[this.dataForm.delivery_index].optimization_goal=='CLICK'&&this.dataForm.adgroup.delivery[this.dataForm.delivery_index].bid_type=='BID_TYPE_CUSTOM'){
                if(!this.dataForm.adgroup.delivery[this.dataForm.delivery_index].bid_price){
                    this.$message.error("请输入成本上限");
                    return;
                }
            }
            if((this.dataForm.adgroup.delivery[this.dataForm.delivery_index].optimization_goal=='CONVERT'||this.dataForm.adgroup.delivery[this.dataForm.delivery_index].optimization_goal=='CONVERT1')&&this.dataForm.adgroup.delivery[this.dataForm.delivery_index].bid_type=='BID_TYPE_CUSTOM'){
                if(!this.dataForm.adgroup.delivery[this.dataForm.delivery_index].conversion_bid_price){
                    this.$message.error("请输入成本上限");
                    return;
                }
            }
            this.$emit("addBulkBid")
        }
    },
};
</script>


<style scoped>
/deep/ .tk_select .el-input {
    width: 86px;
}
/deep/ .tk_input .el-input-group__prepend {
    background-color: #fff;
}
.red{
    font-size: 12px;
    color: #ff3c31;
    margin-left: 10px;
}
.red .el-icon-warning-outline{
    font-size: 14px;
    margin-right: 5px;
}
</style>

